#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <signal.h>

static int fd;

static void sigio_signal_func(int signum)
{
    unsigned int key_val=0;
    read(fd, &key_val, sizeof(unsigned int));
    if(key_val==0){
        printf("Key Pressed!\n");
    }
    else if(key_val==1){
        printf("Key Released!\n");
    }
}

int main(int argc, char *argv[])
{
    int flags=0;

    fd=open(argv[1], O_RDWR | O_NONBLOCK);
    if(fd<0){
        printf("ERROR: file %s open failed!\n", argv[1]);
        return -1;
    }

    signal(SIGIO, sigio_signal_func);
    fcntl(fd, F_SETOWN, getpid());
    flags=fcntl(fd, F_GETFD);
    fcntl(fd, F_SETFL, flags | FASYNC);

    for(;;){
        sleep(2);
    }

    close(fd);
    return 0;
}